package com.redfox.newapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.redfox.newapp.R;
import com.redfox.newapp.activity.PhotoActivity;

import java.util.ArrayList;


/**
 * Created by redfox on 2017/3/15.
 */

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder>
        implements Handler.Callback {

    private final String TAG = "PhotoAdapter";

    private Context mContext;
    private ArrayList<PhotoItem> mItems;


    public static class PhotoItem {
        public String url;
        int width;
        int height;
    }

    public static class PhotoViewHolder extends RecyclerView.ViewHolder {

        public PhotoViewHolder(View itemView) {
            super(itemView);
        }
    }


    public PhotoAdapter(Context context) {
        mContext = context;
        mItems = new ArrayList<PhotoItem>();
    }

    @Override
    public boolean handleMessage(Message msg) {
        return false;
    }

    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.photo_item_view, parent, false);
        return new PhotoViewHolder(v);
    }

    @Override
    public void onBindViewHolder(PhotoViewHolder holder, int position) {
        final PhotoItem item = mItems.get(position);
        RequestListener listener = null;
        ImageView imageView = (ImageView) holder.itemView.findViewById(R.id.photo_thumb);

        if (item.width != 0 && item.height != 0) {
            ViewGroup.LayoutParams params = imageView.getLayoutParams();
            params.width = item.width;
            params.height = item.height;
            imageView.setLayoutParams(params);
        }


        if (item.width == 0 && item.height == 0)
            listener = new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    item.width = resource.getIntrinsicWidth();
                    item.height = resource.getIntrinsicHeight();
                    Log.d(TAG, "onResourceReady: width = " + item.width + ", height = " + item.height + ", isFirstResource = " + isFirstResource);
                    return false;
                }
            };


        Glide.with(mContext)
                .load(item.url)
                .centerCrop()
                .fitCenter()
                .dontAnimate()
                .placeholder(R.drawable.unknown_people)
                .error(R.drawable.unknown_people)
                .crossFade()
                .listener(listener)
                .into(imageView);

        final String url = item.url;

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PhotoActivity.class);
                intent.putExtra("url", url);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public void addItem(PhotoItem item) {
        mItems.add(item);
    }


}
